﻿@using Xms.Abp.Data
@inherits AbpAntComponentBase
@typeparam TPagingListViewModel
@typeparam TKey
@typeparam TPagedParameter
@typeparam TPermissionConsts

@{
    Dictionary<string, int> gutter = new()
    {
        ["xs"] = 8,
        ["sm"] = 16,
        ["md"] = 24,
        ["lg"] = 32,
        ["xl"] = 48,
        ["xxl"] = 64
    };
}

<PageWrapper Class="@Class" Id="Id" RefBack="RefBack" Style="@Style">
    <Breadcrumb>
        <Breadcrumb>
            <BreadcrumbItem>@ModuleName</BreadcrumbItem>
            <BreadcrumbItem>@Title</BreadcrumbItem>
        </Breadcrumb>
    </Breadcrumb>
    <SearchPanel>
        <Row Gutter="@gutter">
            <Col Span="12">
                @if (Creatable && HasCreatePermission)
                {
                    <Button Icon="plus" OnClick='_ => OnToolbarActionClickAsync("Create")'>创建</Button>
                }
                @if (Exportable && HasExportPermission)
                {
                    <Button Icon="export" OnClick='_ => OnToolbarActionClickAsync("Export")'>导出数据</Button>
                }
                @if (DeleteManyable && HasDeleteManyPermission || Importable && HasImportPermission || ToolbarMoreActions != null)
                {
                    <DropdownButton>
                        <Overlay>
                            <Menu>
                                @if (DeleteManyable && HasDeleteManyPermission)
                                {
                                    <MenuItem OnClick='_ => OnToolbarActionClickAsync("DeleteMany")'>批量删除</MenuItem>
                                }
                                @if (Importable && HasImportPermission)
                                {
                                    <MenuItem OnClick='_ => OnToolbarActionClickAsync("DownloadTemplate")'>下载导入模板</MenuItem>
                                    <MenuItem OnClick='_ => OnToolbarActionClickAsync("Import")'>导入数据</MenuItem>
                                }
                                @if(ToolbarMoreActions != null)
                                {
                                    @ToolbarMoreActions
                                }
                            </Menu>
                        </Overlay>
                        <Unbound>
                            批量操作
                        </Unbound>
                    </DropdownButton>
                }
            </Col>
            @if (!AdvanceSearching)
            {
            <AntDesign.Col Span="12">
                <Input TValue="string" @bind-Value="PagedParameter.Filter" Placeholder="模糊搜索内容" />
                <Button Type="@ButtonType.Primary" Icon="search" OnClick="OnSearchClickAsync">搜索</Button>
                @if (AdvanceSearch != null)
                {
                    <Button Type="@ButtonType.Link" Icon="search" OnClick="_=>AdvanceSearching=true">高级搜索</Button>
                }
            </AntDesign.Col>
            }
        </Row>
        @if (AdvanceSearch != null && AdvanceSearching)
        {
            @if(Creatable && HasCreatePermission || Exportable && HasExportPermission || DeleteManyable && HasDeleteManyPermission || Importable && HasImportPermission || ToolbarMoreActions != null)
            {
                <Divider />
            }
            <Row Gutter="@gutter">
                <Col Span="24" ChildContent="AdvanceSearch(PagedParameter)" />
            </Row>
            <Row Gutter="@gutter">
                <AntDesign.Col Span="24">
                    <Button Type="@ButtonType.Primary" Icon="search" OnClick="OnSearchClickAsync">搜索</Button>
                    <Button Type="@ButtonType.Link" Icon="search" OnClick="_=>AdvanceSearching=false">模糊搜索</Button>
                </AntDesign.Col>
            </Row>
        }
    </SearchPanel>
    <Content>
        <Table
            TItem="TPagingListViewModel"
            Bordered="true"
            Size="TableSize.Small"
            RowExpandable="IsTableRowExpandable()"
            DataSource="DataSource"
            Loading="PagingLoading"
            RemoteDataSource>
            <RowTemplate>
                <Selection Key="@(context.Id.ToString())" Disabled="IsSelectionDisabled(context)" Fixed="left" />
                @if (Columns != null)
                {
                    @Columns(context)
                }
                else
                {
                    foreach(var column in GetTableColumnInfos())
                    {
                    <Column TData="object" DataIndex="@column.Name">
                        <ChildContent>
                            @column.GetValue(context)
                        </ChildContent>
                    </Column>
                    }
                }
                <ActionColumn Title="Action" Fixed="right">
                    <Space>
                    @if (Columns != null && context is IEditingInRow e && (e.IsCreating || e.IsUpdating))
                    {
                        if (e.IsCreating)
                        {
                        <SpaceItem><Button Type="@ButtonType.Primary" OnClick='_ => OnRowActionClickAsync("SaveCreate", context)' @bind-Loading="e.IsSaving" @bind-Disabled="e.IsSaving">Save</Button></SpaceItem>
                        <SpaceItem>
                            <Popconfirm Title="Sure to cancel?" OnConfirm='_ =>  OnRowActionClickAsync("CancelCreate", context)' OkText="Yes" CancelText="No">
                                <Button>Cancel</Button>
                            </Popconfirm>
                        </SpaceItem>
                        }
                        else
                        {
                        <SpaceItem><Button Type="@ButtonType.Primary" OnClick='_ => OnRowActionClickAsync("SaveEdit", context)' @bind-Loading="e.IsSaving" @bind-Disabled="e.IsSaving">Save</Button></SpaceItem>
                        <SpaceItem>
                            <Popconfirm Title="Sure to cancel?" OnConfirm='_ => OnRowActionClickAsync("CancelEdit", context)' OkText="Yes" CancelText="No">
                                <Button>Cancel</Button>
                            </Popconfirm>
                        </SpaceItem>
                        }
                    }
                    else
                    {
                        <SpaceItem><Button OnClick='_ => OnRowActionClickAsync("Details", context)'>Details</Button></SpaceItem>
                        <SpaceItem><Button Type="@ButtonType.Primary" OnClick='_=> OnRowActionClickAsync("Edit", context)'>Edit</Button></SpaceItem>
                        <SpaceItem><Button Danger OnClick='_ => OnRowActionClickAsync("Delete", context)'>Delete</Button></SpaceItem>
                        @if (RowMoreActions != null)
                        {
                            <DropdownButton>
                                <Overlay>
                                    <Menu>
                                    @RowMoreActions(context)
                                    </Menu>
                                </Overlay>
                                <Unbound Context="ctx">
                                    更多操作
                                </Unbound>
                            </DropdownButton>
                        }
                    }
                    </Space>
                </ActionColumn>
            </RowTemplate>
            <ExpandTemplate>
            @if (context is IHasRemark s && !s.Remark.IsNullOrWhiteSpace())
            {
                @s.Remark
            }
            </ExpandTemplate>
            <PaginationTemplate>
                <Pagination
                    ShowTotal="PagingShowTotal()"
                    DefaultCurrent="1"
                    @bind-Total="TotalCount"
                    @bind-Current="CurrentPageIndex"
                    @bind-PageSize="CurrentPageSize"
                    TotalBoundaryShowSizeChanger="10"
                    OnChange="OnPagingChange"
                    Size="small"
                    ShowQuickJumper
                    ShowSizeChanger />
            </PaginationTemplate>
        </Table>
    </Content>
</PageWrapper>
